Spring Security

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๊ฐ€ ๋ญ์•ผ?

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๋Š” ์ธ์ฆ (Authentication), ๊ถŒํ•œ(Authorize) ๋ถ€์—ฌ ๋ฐ ๋ณดํ˜ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋‹ค.

Java / Java EE ํ”„๋ ˆ์ž„์›Œํฌ

๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ ๋ณด์•ˆ ๋ถ„์•ผ๋Š” ์‹œ๊ฐ„์ด ๋งŽ์ด ์†Œ์š”๋˜๋Š” ํ™œ๋™๋“ค ์ค‘ ํ•˜๋‚˜๋‹ค. Spring Security๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์งœ์—ฌ์ง„ ๋‚ด๋ถ€ ๋กœ์ง์„ ํ†ตํ•ด ์ธ์ฆ, ๊ถŒํ•œ ํ™•์ธ์— ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๊ณผ ์˜ต์…˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.

์ธ์ฆ(Authentication), ์ธ๊ฐ€(Authorization)

์ธ์ฆ๊ณผ ์ธ๊ฐ€๋ž€ ๋ฌด์—‡์ผ๊นŒ? ๋ณดํ†ต ์ธ์ฆ ์ ˆ์ฐจ๋ฅผ ๊ฑฐ์นœ ํ›„ ์ธ๊ฐ€ ์ ˆ์ฐจ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

  • ์ธ์ฆ : ํ•ด๋‹น ์‚ฌ์šฉ์ž๊ฐ€ ๋ณธ์ธ์ด ๋งž๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ.
  • ์ธ๊ฐ€ : ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญ๋œ ์ž์›์— ์ ‘๊ทผ๊ฐ€๋Šฅํ•œ๊ฐ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์ ˆ์ฐจ

์ธ์ฆ ๋ฐฉ์‹

  1. Credential ๋ฐฉ์‹ : username, password๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹
  2. ์ด์ค‘ ์ธ์ฆ(Twofactor ์ธ์ฆ) : ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐœ์ธ ์ •๋ณด๋ฅผ ์ธ์ฆ ํ›„, ๋‹ค๋ฅธ ์ธ์ฆ ์ฒด๊ณ„(์˜ˆ: ๋ฌผ๋ฆฌ์ ์ธ ์นด๋“œ)๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‘ ๊ฐ€์ง€์˜ ์กฐํ•ฉ์œผ๋กœ ์ธ์ฆํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
  3. ํ•˜๋“œ์›จ์–ด ์ธ์ฆ : ์ž๋™์ฐจ ํ‚ค์™€ ๊ฐ™์€ ๋ฐฉ์‹

์ด์ค‘ Spring Security๋Š” Credential ๊ธฐ๋ฐ˜์˜ ์ธ์ฆ์„ ์ทจํ•ฉ๋‹ˆ๋‹ค.

  • Principal : ์•„์ด๋”” (username)
  • Credential : ๋น„๋ฐ€๋ฒˆํ˜ธ (password)

ํŠน์ • ์ž์›์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ถŒํ•œ์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

ํŠน์ • ๊ถŒํ•œ์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ ์œ ์ €๋Š” ์ธ์ฆ์ •๋ณด(Authentication)๊ฐ€ ํ•„์š”ํ•˜๊ณ  ๊ด€๋ฆฌ์ž๋Š” ํ•ด๋‹น ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•ด ๊ถŒํ•œ์„ ์ธ๊ฐ€(Authorization) ํ•ฉ๋‹ˆ๋‹ค.

๋ณดํŽธ์ ์œผ๋กœ username - password ํŒจํ„ด์˜ ์ธ์ฆ๋ฐฉ์‹์„ ๊ฑฐ์น˜๊ธฐ ๋•Œ๋ฌธ์— ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๋Š” principal - credential ํŒจํ„ด์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Spring Security์˜ ํŠน์ง•

  • Filter๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
    • Spring MVC์™€ ๋ถ„๋ฆฌ๋˜์–ด ๊ด€๋ฆฌํ•˜๊ณ  ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Bean์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • Spring security 3.2๋ถ€ํ„ฐ๋Š” XML ์„ค์ •์„ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์•„ํ‚คํ…์ฒ˜ Spring Security Architecture

์Šคํฌ๋ฆฐ์ƒท 2024-10-16 16-03-33.png

  1. Http ์š”์ฒญ :
    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์‚ฌ์šฉ์ž ์ž๊ฒฉ ์ฆ๋ช…(์˜ˆ: ์‚ฌ์šฉ์ž ์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ)์„ ํฌํ•จํ•˜๋Š” HTTP ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค. ์ด ์š”์ฒญ์€ ์ธ์ฆ ํ•„ํ„ฐ(Authentication Filter)๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
  2. AuthenticationFilter ์ธ์ฆ ํ•„ํ„ฐ :
    • ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„๊ณ  ์ž๊ฒฉ ์ฆ๋ช…์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ผ๋ฐ˜์ ์œผ๋กœ UsernamePasswordAuthenticationToken๊ณผ ๊ฐ™์€ Authentication ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  3. AuthenticationManager ์ธ์ฆ ๊ด€๋ฆฌ์ž :
    • ์ธ์ฆ ํ•„ํ„ฐ๋Š” AuthenticationManger ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. AuthenticationManager๋Š” ์ธ์ฆ ํ”„๋กœ์„ธ์Šค๋ฅผ ์—ฌ๋Ÿฌ AuthenticationProvider์—๊ฒŒ ์œ„์ž„ํ•˜๋Š” ์กฐ์ •์ž ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  4. AuthenticationProvider ์ธ์ฆ ์ œ๊ณต์ž :
    • ์ธ์ฆ ๊ด€๋ฆฌ์ž ์ธ์ฆ ์š”์ฒญ์„ ์—ฌ๋Ÿฌ AuthenticationProvider ์ธ์Šคํ„ด์Šค์— ์œ„์ž„ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ œ๊ณต์ž๋Š” ์‹ค์ œ ์ธ์ฆ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ: ์‚ฌ์šฉ์ž ์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ™•์ธ)
  5. UserDetailsService :
    • DaoAuthenticationProvider์™€ ๊ฐ™์€ ์ธ์ฆ ์ œ๊ณต์ž ์ค‘ ํ•˜๋‚˜๋Š” UserDetailsService๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. UserDetailsService๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋˜๋Š” ๋‹ค๋ฅธ ์˜๊ตฌ ์ €์žฅ์†Œ์—์„œ ์‚ฌ์šฉ์ž์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋ฉฐ, ๋ณดํ†ต UserDetails ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  6. UserDetails ์ธํ„ฐํŽ˜์ด์Šค :
    • UserDetails ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ด๋Š” User ๊ฐ์ฒด์— ์˜ํ•ด ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ์‚ฌ์šฉ์ž ์ด๋ฆ„, ๋น„๋ฐ€๋ฒˆํ˜ธ, ์—ญํ• (๊ถŒํ•œ)์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.
  7. ์‚ฌ์šฉ์ž ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉ์ž ๋กœ๋“œ :
    • UserDetailsService๋Š” ์‚ฌ์šฉ์ž ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉ์ž์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋กœ์ง์„ ๊ตฌํ•˜๋…€ํ•˜๊ณ , ์‚ฌ์šฉ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” UserDetails ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  8. AuthenticationProvider ์ธ์ฆ ์ œ๊ณต์ž :
    • ์‚ฌ์šฉ์ž์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋กœ๋“œ๋˜๋ฉด ์ธ์ฆ ์ œ๊ณต์ž๋Š” ์ž๊ฒฉ ์ฆ๋ช…์ด ์œ ํšจํ•œ์ง€ ํ™•์ž…ํ•ฉ๋‹ˆ๋‹ค(์˜ˆ: ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋น„๊ต). ์œ ํšจํ•˜๋‹ค๋ฉด ์ธ์ฆ์ด ์„ฑ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  9. ์„ฑ๊ณต / ์‹คํŒจ :
    • ์ธ์ฆ์ด ์„ฑ๊ณตํ•˜๋ฉด ์ธ์ฆ ๊ด€๋ฆฌ์ž๋Š” ์ธ์ฆ๋œ Authentication ๊ฐ์ฒด๋ฅผ AuthenticationFilter๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ž๊ฒฉ ์ฆ๋ช…์ด ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด AuthenticationException์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  10. SecurityContextHolder :
    • ์ธ์ฆ์ด ์„ฑ๊ณตํ•˜๋ฉด ์ธ์ฆ๋œ Authentication ๊ฐ์ฒด๋Š” SecurityContextHolder์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. SecurityContextHolder๋Š” ํ˜„์žฌ ์š”์ฒญ์— ๋Œ€ํ•œ Authentication ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๋Š” SecurityContext๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ •์  ํ—ฌํผ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

์ด ์•„ํ‚คํ…์ฒ˜๋Š” Spring Security๊ฐ€ ์‚ฌ์šฉ์ž ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์„ ์„ค๋ช…ํ•˜๋ฉฐ, HTTP ์š”์ฒญ์—์„œ ์‹œ์ž‘ํ•ด ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋‹ค์–‘ํ•œ ๊ตฌ์„ฑ ์š”์†Œ์™€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฑฐ์นฉ๋‹ˆ๋‹ค.